#include "enemy2.h"
#include "config.h"
Enemy2::Enemy2()
{
    //加载资源
    m_Plane.load(ENEMY2_PATH);
    m_PlaneHit.load(ENEMY2_PATH2);
    m_PlaneDown[0].load(ENEMY2_DOWN1);
    m_PlaneDown[1].load(ENEMY2_DOWN2);
    m_PlaneDown[2].load(ENEMY2_DOWN3);
    m_PlaneDown[3].load(ENEMY2_DOWN4);
    m_PlaneDown[4].load(ENEMY2_DOWN5);
    m_PlaneDown[5].load(ENEMY2_DOWN6);
    m_PlaneDown[6].load(ENEMY2_DOWN7);
    p_Plane = &m_Plane;

    //坐标
    m_X = 0;
    m_Y = 0;

    //速度
    m_Speed = ENEMY2_SPEED;

    //矩形框
    m_Rect.setWidth(m_Plane.width());
    m_Rect.setHeight(m_Plane.height());
    m_Rect.moveTo(m_X, m_Y);

    //是否闲置
    m_Free = true;

    //爆炸状态
    m_Down = -1;

    //间隔
    m_Interval = 0;

    //生命
    m_life = ENEMY2_LIFE;
}

/* 更新敌机坐标 */
void Enemy2::updatePosition()
{
    //更新子弹
    for(int i=0; i < BULLET2_NUM; i++)
    {
        m_Bullets[i].updatePosition();
    }
    if(m_Free)
    {
        return;
    }
    //爆炸处理
    if(m_Down >= 0)
    {
        if(m_Down % ENEMY2_DOWN_INTERVAL == 0)
        {
            if(m_Down >= ENEMY2_DOWN_NUM*ENEMY2_DOWN_INTERVAL)
            {
                p_Plane = &m_Plane;
                m_Down = -1;
                m_Free = true;
                return;
            }
            else
            {
                p_Plane = &m_PlaneDown[m_Down/ENEMY2_DOWN_INTERVAL];
            }
        }
        m_Down++;
        return;
    }

    //射击
    shoot();

    m_Y += m_Speed;
    m_Rect.moveTo(m_X, m_Y);

    if(m_Y > GAME_HEIGHT)
    {
        m_Free = true;
    }
}
/* 射击与图片切换 */
void Enemy2::shoot()
{
    m_Interval++;
    if(m_Interval == ENEMY2_CYCLE/ENEMY2_PIC_NUM)
    {
        p_Plane = &m_PlaneHit;
        //射击
        for(int i=0; i < BULLET2_NUM; i++)
        {
            //寻找空闲子弹并发射
            if(m_Bullets[i].m_Free)
            {
                //将子弹空闲状态改为假
               m_Bullets[i].m_Free = false;
                //设置发射的子弹坐标
               m_Bullets[i].m_X = m_X + m_Rect.width()*0.5 - 10;
               m_Bullets[i].m_Y = m_Y - 25 ;
               break;

            }
        }
    }
    if(m_Interval >= ENEMY2_CYCLE)
    {
        p_Plane = &m_Plane;
        m_Interval = 0;
    }
}

